package plugins;

import java.util.HashMap;
import java.util.Map;

import Utils.Utils;

import plugins.flavours.RollPlugin;
import plugins.flavours.SmsPlugin;
import plugins.system.GetRankNamePlugin;
import plugins.system.JumpRoomPlugin;
import plugins.system.PersonalMessagePlugin;
import plugins.system.PluginListPlugin;
import plugins.system.QuitPlugin;
import plugins.system.RoomsListPlugin;
import plugins.system.SendRoomMsgPlugin;
import plugins.system.SendServerMsgPlugin;
import plugins.system.UserlistPlugin;
import plugins.system.WrongCommandParameterPlugin;

import main.Room;
import main.User;

public class PluginModule {
   private Map<String[], Plugin> commandMap;
   private WrongCommandParameterPlugin wrongCommandPlug;
   
   public PluginModule() {
      commandMap = new HashMap<String[], Plugin>();
      
      String[] pluginListPlug = {"/plugins", "/plug", "/plugs", "/commands"};
      commandMap.put(pluginListPlug, new PluginListPlugin());
      String[] userListPlug = {"/ls", "/list"};
      commandMap.put(userListPlug, new UserlistPlugin());
      String[] quitPlug = {"/q", "/quit", "/exit", "/end"};
      commandMap.put(quitPlug, new QuitPlugin());
      String[] roomListPlug = {"/room"};
      commandMap.put(roomListPlug, new RoomsListPlugin());
      String[] rollPlug = {"/roll", "/dice", "/rnd", "/random"};
      commandMap.put(rollPlug, new RollPlugin());
      String[] personalMessagePlug = {"/pm", "/w", "/whisper"};
      commandMap.put(personalMessagePlug, new PersonalMessagePlugin());
      String[] smsPlug = {"/sms", "/SMS"};
      commandMap.put(smsPlug, new SmsPlugin());
      String[] jumpPlug = {"/jump", "/go", "/changeroom"};
      commandMap.put(jumpPlug, new JumpRoomPlugin());
      String[] getRankNamePlug = {"/rank"};
      commandMap.put(getRankNamePlug, new GetRankNamePlugin());
      String[] sendRoomMsgPlug = {"/roommsg"};
      commandMap.put(sendRoomMsgPlug, new SendRoomMsgPlugin());
      String[] sendServerMsgPlug = {"/servermsg"};
      commandMap.put(sendServerMsgPlug, new SendServerMsgPlugin());
      
      wrongCommandPlug = new WrongCommandParameterPlugin();
   }
   
   /**
    * Calls and checks the command
    * @return true if command was executed, false if command was false
    */
   public boolean call(String line, Room server, User serverThread) {
      if (line.charAt(0) != '/') {
         return false; //the line is not a command
      }
      String command = line.split(" ")[0];
      for (String[] keyList : commandMap.keySet()) {
         for (String key : keyList) {
            if (command.equals(key)) {
               commandMap.get(keyList).execute(server, serverThread, command, Utils.cutWordFromString(line, 0));
               return true;
            }
         }
      }
      wrongCommandPlug.execute(server, serverThread, command, Utils.cutWordFromString(line, 0));
      return true;
   }

   public Map<String[], Plugin> getCommandMap() {
      return commandMap;
   }
}
